package com.hengpeng.msls.service.userInfo.impl;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import com.hengpeng.common.persist.entity.AbstractEntity.BoolValue;
import com.hengpeng.common.persist.query.DynamicQuery;
import com.hengpeng.itfinbase.constants.TransCodeConstants;
import com.hengpeng.itfinbase.message.ResponseMessage;
import com.hengpeng.itfinbase.message.partner.UserBankCardRequest;
import com.hengpeng.itfinbase.persist.UserBankCard;
import com.hengpeng.msls.service.BaseService;
import com.hengpeng.msls.service.userInfo.UserBankCardService;

/**
 * <pre>
 * 银行卡管理
 * </pre>
 * 
 * @author shanguogang
 * @version 1.0, 2015-8-6
 */
@Service
public class UserBankCardServiceImpl extends BaseService implements UserBankCardService {
    /**
     * <pre>
     * 查询银行卡
     * </pre>
     * 
     * @param userName
     * @param bankCardId
     * @param isPriority
     * @return
     */
    public List<UserBankCard> findBankCard(String userName, Long bankCardId, BoolValue isPriority) {
        DynamicQuery query = new DynamicQuery(UserBankCard.class);
        if (bankCardId != null) {
            query.eq("id", bankCardId);
        }
        if (!StringUtils.isEmpty(userName)) {
            query.eq("userName", userName);
        }
        if (isPriority != null) {
            query.eq("isPriority", isPriority);
        }
        query.desc("isPriority");
        query.desc("createTime");
        return serviceFactory.getProtFactory().queryList(query, UserBankCard.class);
    }

    /**
     * <pre>
     * 查询银行卡
     * </pre>
     * 
     * @param userName
     * @return
     */
    @Override
    public List<UserBankCard> findBankCard(String userName) {
        return this.findBankCard(userName, null, null);
    }

    /**
     * <pre>
     * 添加银行卡
     * </pre>
     * 
     * @param userBankCard
     * @return
     */
    public ResponseMessage saveUserBankCard(UserBankCard userBankCard) {
        UserBankCardRequest request = new UserBankCardRequest();
        request.setBankCard(userBankCard);
        request.setTransType(TransCodeConstants.MemberTransCode.MEMBER_BANKCARD_SAVE);
        return portFactory.remoteTrans(request);
    }

    /**
     * <pre>
     * 绑定银行卡
     * </pre>
     * 
     * @param userBankCard
     * @return
     */
    public ResponseMessage userBankCard(UserBankCard userBankCard) {
        UserBankCardRequest request = new UserBankCardRequest();
        request.setBankCard(userBankCard);
        request.setTransType(TransCodeConstants.MemberTransCode.MEMBER_BANKCARD_BD);
        return portFactory.remoteTrans(request);
    }

    /**
     * <pre>
     * 修改银行卡
     * </pre>
     * 
     * @param userBankCard
     * @return
     */
    public ResponseMessage updateUserBankCard(UserBankCard userBankCard) {
        UserBankCardRequest request = new UserBankCardRequest();
        request.setBankCard(userBankCard);
        request.setTransType(TransCodeConstants.MemberTransCode.MEMBER_BANKCARD_UPDATE);
        return portFactory.remoteTrans(request);
    }

    /**
     * <pre>
     * 删除银行卡
     * </pre>
     * 
     * @param userBankCard
     * @return
     */
    public ResponseMessage deleteUserBankCard(UserBankCard userBankCard) {
        UserBankCardRequest request = new UserBankCardRequest();
        request.setBankCard(userBankCard);
        request.setTransType(TransCodeConstants.MemberTransCode.MEMBER_BANKCARD_REMOVE);
        return portFactory.remoteTrans(request);
    }

}
